package vn.cuhanh.dialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import vn.cuhanh.MainActivity;
import vn.cuhanh.R;
import vn.cuhanh.utils.HelperFunc;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

public class RatingDialog extends Activity {
	private static String GA_CATEGORY = "rating_dialog";
	
	private int heroId;
	private int resultCode;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rating_hero);
		heroId = getIntent().getExtras().getInt("heroId");
		Button btnSave = (Button) findViewById(R.id.rating_dialog_btn_save);
		btnSave.setText(R.string.save);
		Button btnClose = (Button) findViewById(R.id.rating_dialog_btn_close);
		btnClose.setText(R.string.close);
//		btnClose.setBackground(getResources().getDrawable(R.drawable.btn_grey_bg));
		btnClose.setBackgroundResource(R.drawable.btn_grey_bg);
		resultCode = RESULT_CANCELED;
		
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				HelperFunc.trackEvent(RatingDialog.this, GA_CATEGORY, "btn_save_press", "btn_save_press");
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						postRating();
					}
				});
				thread.start();
			}
		});
		btnClose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				HelperFunc.trackEvent(RatingDialog.this, GA_CATEGORY, "btn_close_press", "btn_close_press");
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		setResult(resultCode);
		finish();
	}
	
	public void postRating() {
		RatingBar ratingBar = (RatingBar)findViewById(R.id.hero_detail_rating_bar);
		int value = Math.round(ratingBar.getRating());
		int messageId = 0;
		try {
			/*ChoApp choApp = ((ChoApp) ctx.getApplicationContext());*/
			String username = MainActivity.username;
			String sessionid = MainActivity.sessionId;
			List<NameValuePair> ajaxParams = new ArrayList<NameValuePair>(4);
			ajaxParams.add(new BasicNameValuePair("heroId", heroId + ""));
			ajaxParams.add(new BasicNameValuePair("value", value + ""));
			ajaxParams.add(new BasicNameValuePair("username", username));
			ajaxParams.add(new BasicNameValuePair("sessionId", sessionid));

			String svResponse = HelperFunc.getDataFromSv(getResources()
					.getString(R.string.postRatingSsoUrl), ajaxParams);
			if (svResponse != null) {
				Log.d("postRatingResponse", svResponse);
				JSONObject rs = new JSONObject(svResponse);

				switch (rs.getInt("status")) {
				case 1:
					messageId = R.string.rating_success;
					resultCode = RESULT_OK;
					break;

				case -1:
					messageId = R.string.rating_fail_rated;
					break;

				case 0:
					messageId = R.string.rating_fail_rated;
					break;

				case 9000:
					/*
					 * case 9001: ((Activity) dlgCtx).runOnUiThread(new
					 * Runnable() { public void run() { Intent i = new
					 * Intent(((Activity) dlgCtx), LoginDialog.class);
					 * startActivity(i); } }); break;
					 */
				default:
					messageId = R.string.rating_fail_server_busy;
					break;
				}
			} else {
				messageId = R.string.no_network_2;
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			HelperFunc.readStackTrace(e);
			messageId = R.string.rating_fail_server_busy;

		}
		if (messageId != 0) {
			final String strTemp = getResources().getString(messageId);
			RatingDialog.this.runOnUiThread(new Runnable() {
				public void run() {
//					HelperFunc.showSimpleAlertDialog(RatingDialog.this, null, strTemp, "", new android.content.DialogInterface.OnClickListener() {
//						
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							// TODO Auto-generated method stub
//							onBackPressed();
//						}
//					}, null, null, true);
					Toast.makeText(RatingDialog.this, strTemp, Toast.LENGTH_SHORT).show();
//					Timer timer = new Timer();
				}
			});
		}
		RatingDialog.this.runOnUiThread(new Runnable() {
			public void run() {
				onBackPressed();
			}
		});
	}
}
